import java.util.ArrayList;

public class SelectionPanel extends Component{

	// Text box used to select Nationalities
	TextBoxNationality textBoxNationality;
	// Text box used to select Artists
	TextBoxArtist textBoxArtist;

	// Age selection
	public HorizontalScrollBar timeline;

	// Highlight variables
	int xHighLight,yHighLight;
	String stringHighLight;

	// Gender selection
	GenderButton m,f,age_na;
	
	// Nationalities selected
	ButtonPlusMinus nationalities;
	ScrollBar nationalitiesScrollBar;
	public float nationalitiesTimer=(float)0.0;
	public int heightVal=0;;
	public final int MAX_NATIONALITIES = 6;
	public final int NATIONALITY_HEIGHT = 25;
	public int barHeight=0;


	public SelectionPanel(){
		textBoxNationality = new TextBoxNationality("Search Nationality...",50,16);
		textBoxArtist = new TextBoxArtist("Search Artist...");
		textBoxArtist.x =GLOBAL.width-250-171;//600+155;
		textBoxArtist.y= 70+20;

		// Age selection
		ArrayList<Integer> ages = new ArrayList<Integer>();
		for(int i=10; i<=70; i++)
			ages.add(new Integer(i));
		timeline = new HorizontalScrollBar(ages);
		timeline.x = this.x + 680;
		timeline.y = y+12.5f;
		timeline.width = 250;
		timeline.height = 30;
		timeline.size = (float)2/ages.size();
		
		// Gender
		m = new GenderButton(x + 480, 20);
		m.state = true;
		f = new GenderButton(x + 540, 20);
		f.state = true;
		
		// Nationalities selected
		nationalities = new ButtonPlusMinus("Nationalities:");
		nationalities.x = 260;
		nationalities.y = 23;
		
		nationalitiesScrollBar = new ScrollBar();
		
		age_na = new GenderButton(x + 960, 20);
		age_na.state = true;
	
	}

	public void draw(){
		stringHighLight = null;
		deactivateButtons();
		drawBackGround();
		textBoxNationality.draw();
		timeline.draw();
		
		m.draw();
		f.draw();
		age_na.draw();
		
		gendersLabels();
		
		drawNationalities();
		drawHighLight();

	}
	
	
	public void drawNationalities(){
		nationalities.draw();
		if(!nationalities.pluState){
			if(nationalitiesTimer==(float)0.0){
				nationalitiesTimer = GLOBAL.timer;
				heightVal=0;
			}
			heightVal = (int)GLOBAL.processing.lerp(heightVal,barHeight,(float) 0.3);
			drawBar();
		}
		else{if(nationalitiesTimer!=(float)0.0){
			heightVal=height;
		}
		nationalitiesScrollBar.val=0;
		heightVal = (int)GLOBAL.processing.lerp(heightVal,0,(float) 0.3);
		drawBar();
		nationalitiesTimer=(float)0.0;
		}
	}
	
	
	private void drawBar(){

		if(GLOBAL.analysisType == 2 || GLOBAL.analysisType == 3) return;


		int size = GLOBAL.nationalitiesButtonSelected.size();
		if(size<MAX_NATIONALITIES)
			barHeight= size*NATIONALITY_HEIGHT+5;
		else barHeight=NATIONALITY_HEIGHT*MAX_NATIONALITIES;
		if(heightVal==0) return;
		if(size==0) return;

		GLOBAL.processing.fill(GLOBAL.colorBackground,200);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.rect(nationalities.x-5, nationalities.y+20, 150, heightVal);
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.fill(GLOBAL.textColor);	
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);

		float j;
		if(size>MAX_NATIONALITIES &&heightVal>20){
			nationalitiesScrollBar.width = 15;
			nationalitiesScrollBar.x = nationalities.x+120+10;
			nationalitiesScrollBar.y = nationalities.y+20;
			nationalitiesScrollBar.height = this.heightVal-15;
			nationalitiesScrollBar.draw();  
			nationalitiesScrollBar.size = 1/(float)((float)size/(MAX_NATIONALITIES-1));
			j = Main.map(nationalitiesScrollBar.val,0,1,0,GLOBAL.nationalitiesSelected.size());
		}
		else j=0;
		int start = (int)(j+0.5);
		for(int i=start; i<GLOBAL.nationalitiesButtonSelected.size() && i<=start+MAX_NATIONALITIES+2 && i<start+heightVal/24;i++){
			CloseButtonNationality n = GLOBAL.nationalitiesButtonSelected.get(i);
			if(n.clicked){
				nationalitiesScrollBar.val=0;
				n.clicked = false;
				n.active = true;
				GLOBAL.nationalityAddRemove(n.nationality);
			}
			else{
				n.x=nationalities.x+5;
				n.y=nationalities.y+25+(i-start)*24;
				n.active = true;
				n.draw();

			}
		}

	}
	
	private void deactivateButtons() {
		for(int i=0;i<GLOBAL.nationalitiesButtonSelected.size();i++){
			GLOBAL.nationalitiesButtonSelected.get(i).active=false;
		}
	}

	private void gendersLabels() {
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
		GLOBAL.processing.textFont(GLOBAL.plotFont,14);
		GLOBAL.processing.fill(GLOBAL.textColor);
		GLOBAL.processing.text("M", m.x - 20, m.y + m.height - 3);
		GLOBAL.processing.text("F", f.x - 20, f.y + f.height - 3);
		GLOBAL.processing.text("Age :", timeline.x - 40, timeline.y + timeline.height - 8);
		GLOBAL.processing.textFont(GLOBAL.plotFont,12);
		GLOBAL.processing.text("Age n.a.", age_na.x + age_na.width/2 - 18, age_na.y + 37);

	}

	private void drawBackGround() {
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.rectMode(GLOBAL.processing.CORNERS);
		GLOBAL.processing.fill(GLOBAL.menuColor);
		GLOBAL.processing.rect(x, y, x+width, y+height);
	}
	
	private void drawHighLight() {
		if(stringHighLight==null) return;
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		int w = (int)(GLOBAL.processing.textWidth(stringHighLight));
		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.noStroke();
		GLOBAL.processing.fill(GLOBAL.colorBackground);
		GLOBAL.processing.rect(xHighLight, yHighLight, w+10, 30);
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.fill(GLOBAL.textColor);
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
		GLOBAL.processing.text(stringHighLight, xHighLight, yHighLight + 20);		
	}

	public void mousePressed() {
		if(GLOBAL.analysisType !=2 && textBoxNationality.scrollBar.mouseOver()){textBoxNationality.scrollBar.mousePressed();}
		else if(GLOBAL.analysisType !=2 && textBoxNationality.mouseOver()){
			if(!textBoxNationality.active){textBoxNationality.active=true;}
			else{textBoxNationality.active=false;}
		}		
		
		else if(GLOBAL.analysisType ==2 && textBoxArtist.active && textBoxArtist.mouseOverSuggestions())textBoxArtist.mousePressed();


		else if(GLOBAL.analysisType !=2 && timeline.mouseOver()) 
			timeline.mousePressed();
		
		// Gender
		else if(GLOBAL.analysisType !=2 && m.mouseOver()) {
			if (GLOBAL.genderSelected == 0) {
				GLOBAL.genderSelected = 2;
				m.state = false;
				GLOBAL.updateFilterResults();
			}
			else if (GLOBAL.genderSelected == 2) {
				GLOBAL.genderSelected = 0;
				m.state = true;
				GLOBAL.updateFilterResults();
			}
		}	
		else if(GLOBAL.analysisType !=2 && f.mouseOver()) {
			if (GLOBAL.genderSelected == 0) {
				GLOBAL.genderSelected = 1;
				f.state = false;
				GLOBAL.updateFilterResults();
			}
			else if (GLOBAL.genderSelected == 1) {
				GLOBAL.genderSelected = 0;
				f.state = true;
				GLOBAL.updateFilterResults();
			}
		}
		else if(GLOBAL.analysisType !=2 && age_na.mouseOver()) {
			GLOBAL.age_undefinedSelected = !GLOBAL.age_undefinedSelected;
			age_na.state = GLOBAL.age_undefinedSelected;
			GLOBAL.updateFilterResults();
		}
		else if (GLOBAL.analysisType !=2 && nationalities.mouseOver()){nationalities.mouseCliked();}
		
	}


	public boolean mouseOver(){

		if(GLOBAL.analysisType !=2 && textBoxNationality.active && textBoxNationality.mouseOverSuggestions()) return true;
		else if(GLOBAL.analysisType == 2 && textBoxArtist.active && textBoxArtist.mouseOverSuggestions()) return true;
		return super.mouseOver();
	}

	public void mouseReleased() {
		textBoxNationality.scrollBar.mouseReleased();
		textBoxArtist.scrollBar.mouseReleased();
		nationalitiesScrollBar.mouseReleased();
		timeline.mouseReleased();
	}

}
